package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupMapper checkGroupMapper;


    /**
     * 检查项组分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult getInquire(QueryPageBean queryPageBean) {
        //添加分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());

        //查询数据列表
        Page<CheckGroup> page = checkGroupMapper.getdata(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(),page.getResult());
    }



    /**
     * 新增数据
     * @return
     */
    @Override
    @Transactional
    public void updata(CheckGroup checkGroup,String id) {
        System.out.println(id);
        //添加检查组
         checkGroupMapper.newlygroup(checkGroup);
        //查询新增检查组id
       int groupid = checkGroupMapper.getnewlygroup(checkGroup);

            //获取每一个检查项id
        if (id!=null||id.length()!=0){
            String[] split = id.split(",");
            for (String everyid : split) {
                //添加对应的id
                checkGroupMapper.setdata(groupid,everyid);
            }

        }
        //添加检查组与检查项id的对应关系



    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public CheckGroup getbyID(Integer id) {
        //查询检查组数据并返回
        CheckGroup checkGroup = checkGroupMapper.getIdData(id);

        return checkGroup;
    }

    @Override
    public List<Integer> getbycheckitmlID(Integer id) {
        //根据检查组id查询对应的检查项
        List<Integer> list = checkGroupMapper.getids(id);

        return list;
    }

    /**
     * 根据检查组id删除数据
     * @param id
     */
    @Override
    @Transactional
    public void deletedata(Integer id) {
        //查看检查组上是否有关联检查套餐数据
        int countid = checkGroupMapper.examine(id);

        //判断是否有套餐数据
        if (countid>0){
            throw new BusinessRuntimeException(MessageConst.DELETE_CHECKGROUP_FAIL);
        }
        //如果没有责删除检查组数据
        checkGroupMapper.daletegroup(id);

        //删除与检查组绑定的检查项
        checkGroupMapper.deletecheckitem(id);

    }

    /**
     * 更新检查组数据
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @Override
    @Transactional
    public PageResult updateDATE(CheckGroup checkGroup, Integer[] checkitemIds) {
        //拿到检查组id
        Integer checkGroupId = checkGroup.getId();

        //讲新的检查组存入表中
        checkGroupMapper.updateDATA(checkGroup);


        if (checkitemIds!=null){
            //批量新增新的检查项
            for (Integer checkitemId : checkitemIds) {
                //删除旧的检查组与检查项对应关系
                checkGroupMapper.deleteAllcheckitem(checkGroupId);

                checkGroupMapper.insertDATA(checkitemId,checkGroupId);
            }

        }


        return null;
    }
}
